package studentinformationmanagementsystem.servlet.admin;

import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.entity.Teacher;
import studentinformationmanagementsystem.service.TeacherService;
import studentinformationmanagementsystem.service.impl.TeacherServiceImpl;
import studentinformationmanagementsystem.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/admin/function/teacher.do")
public class TeacherServlet extends BaseServlet {
    TeacherService teacherService=new TeacherServiceImpl();
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Teacher> teachers = teacherService.queryAll();
        request.setAttribute("teachers",teachers);
        //通过请求转发，将请求对象转发到教师列表jsp上
        request.getRequestDispatcher("/admin/function/teachers.jsp").forward(request,response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        Integer  id= Integer.valueOf(idStr);
        //根据id删除信息
        Teacher teacher = new Teacher();
        teacher.setTeacherid(id);
        int delete = teacherService.delete(teacher);
        response.sendRedirect("./teacher.do?action=queryAll");
    }
    public void insert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Teacher teacher = getParameters(request);
        int result = teacherService.register(teacher);
        response.setContentType("text/html;charset=utf-8");
        if(result!=0){
            //添加教师成功
            HttpSession session = request.getSession();
            session.setAttribute("teacher",result);
            response.getWriter().print(1);
        }
        else {
            response.getWriter().print(0);
        }
    }

    public void queryById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String idStr = request.getParameter("id");
        Integer id = Integer.valueOf(idStr);
        Teacher teacher = new Teacher();
        teacher.setTeacherid(id);
        Teacher teacherinfo = teacherService.queryById(teacher);
        request.getSession().setAttribute("teacherinfo",teacherinfo);
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
        Teacher teacherinfo =(Teacher) request.getSession().getAttribute("teacherinfo");
        Integer id= teacherinfo.getTeacherid();
        Teacher teacher = getParameters(request);
        teacher.setTeacherid(id);
        int result=teacherService.update(teacher);
//        response.sendRedirect("./teacher.do?action=queryAll");
    }
    public Teacher getParameters(HttpServletRequest request){
//        Integer teacherid = request.getParameter("teacherid");
        String teachername = request.getParameter("teachername");
        String idnumber = request.getParameter("idnumber");
        String sex = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String politicsstatus = request.getParameter("politicsstatus");
        String nation = request.getParameter("nation");
        String photograph = request.getParameter("photograph");
        String mailbox = request.getParameter("mailbox");
        Date birthdate = Date.valueOf(request.getParameter("birthdate"));
        String nativeplace = request.getParameter("nativeplace");
        String homeaddress = request.getParameter("homeaddress");
        String teachingage = request.getParameter("teachingage");
        String education = request.getParameter("education");
        Date entrytime = Date.valueOf(request.getParameter("entrytime"));
        String individualresume = request.getParameter("individualresume");
        String password = request.getParameter("password");
        String Teachersworknumber = request.getParameter("Teachersworknumber");
        Teacher teacher = new Teacher(null, teachername, idnumber, sex, phone, politicsstatus, nation, photograph, mailbox, birthdate, nativeplace, homeaddress, teachingage, education, entrytime, individualresume, password,Teachersworknumber);
        return teacher;
    }
}
